package com.wm.blog_web.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.domain.TUserDO;
import com.wm.blog_common.entity.TUser;
import com.wm.blog_common.result.Result;
import com.wm.blog_web.controller.base.WebBaseController;
import com.wm.blog_web.query.TUserQuery;
import com.wm.blog_web.service.TUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wm_yu
 * @since 2020-03-24
 */
@Slf4j
@RestController
@RequestMapping("/web/user")
public class TUserController extends WebBaseController {

    @Autowired
    private TUserService tUserService;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<TUserDO> get(@PathVariable Long id) {
        return tUserService.selectById(id);
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id){
        return tUserService.deleteById(id);
    }

    /**
     * 添加
     * @param tUser
     * @return
     */
    @PostMapping
    public Result insert(@RequestBody TUser tUser){
        return tUserService.insert(tUser);
    }

    /**
     * 修改
     * @param tUser
     * @return
     */
    @PutMapping
    public Result update(@RequestBody TUser tUser){
        return tUserService.update(tUser);
    }

    /**
     * 分页查询
     * @param current   查询页
     * @param size   每页显示条数
     * @param query 查询参数
     * @return
     */
    @GetMapping("/page")
    public Result<Page<TUserDO>> page(Integer current, Integer size, TUserQuery query){
        return tUserService.page(new QueryPage(current,size),query);
    }


    /**
     * 用户注册
     * @param query
     * @return
     */
    @GetMapping("/regist")
    public Result regist(TUserQuery query, HttpServletRequest request){
        return tUserService.regist(query,request);
    }



    @PostMapping("/login")
    public Result login(@RequestBody TUserQuery query, HttpServletRequest request){
        return tUserService.login(query,request);
    }

    /**
     * x修改密码
     * @param query
     * @return
     */
    @PostMapping("/editPassword")
    public Result editPassword(@RequestBody TUserQuery query){
        return tUserService.editPassword(query);
    }



    /**
     * 刷新token
     * @param
     * @return
     */
    @GetMapping("/refresh")
    public Result refresh(String token,Long userId){
        return tUserService.refresh(token,userId);
    }

    /**
     * 查询当前登录人的信息
     * @return
     */
    @GetMapping("/currentInfo")
    public Result currentInfo(HttpServletRequest request){
        Long userId = super.currentUserId(request);
        return tUserService.currentInfo(userId);
    }

}
